<script>
import { mapActions, mapGetters, mapMutations } from "vuex";
import { apiDistributionCode } from "@/api/user";
import { strToParams } from "@/utils/tools";
import { INVITE_CODE } from "@/config/cachekey";
import Cache from "@/utils/cache";
export default {
  async onLaunch(options) {
    console.log(options);
    // 获取公共配置
    await this.getConfig().then((res) => {
      // 当后台配置商城停止后相关商城进行跳转到空页面
      // #ifdef H5
      if (!res.h5_status) {
        setTimeout(() => {
          uni.navigateTo({
            url: "/bundle/pages/business_suspended/business_suspended",
          });
        }, 0);
      }
      // #endif
      // #ifdef MP-WEIXIN
      if (!res.mnp_status) {
        setTimeout(() => {
          uni.navigateTo({
            url: "/bundle/pages/business_suspended/business_suspended",
          });
        }, 0);
      }
      // #endif
    });
    // 获取主题配置
    await this.getDecorateConfig();
    // 获取个人信息
    this.getUser().then((res) => {
      // 更新购物车信息
      this.$store.dispatch("getCartNum");
      if (this.bindMobile && !res.mobile) {
        this.logout();
      }
    });
  },
  onShow: function (options) {
    this.bindCode(options);

    // console.log('App Show')
  },
  onHide: function () {
    console.log("App Hide");
  },
  computed: {
    bindMobile() {
      // 强制绑定手机号
      return this.appConfig.coerce_mobile;
    },
  },
  methods: {
    ...mapActions(["getConfig", "getDecorateConfig", "getUser"]),
    ...mapMutations(["logout"]),
    async bindCode(options) {
      if (!options.query) return;
      let invite_code =
        options.query.invite_code ||
        strToParams(decodeURIComponent(options.query.scene)).invite_code;
      console.log(options);
      if (invite_code) {
        apiDistributionCode({
          code: invite_code,
          hide: 1,
        }).catch(() => {
          Cache.set(INVITE_CODE, invite_code);
        });
      }
    },
  },
};
</script>

<style lang="scss">
/*每个页面公共css */
@import "styles/common.scss";
@import "plugin/emoji-awesome/css/apple.css";
@import "components/uview-ui/index.scss";
</style>
